package hotciv.battlestrategies;

import hotciv.framework.BattleStrategy;
import hotciv.framework.GameContext;
import hotciv.framework.Player;
import hotciv.framework.Position;
import hotciv.standard.Utility;

import java.util.Random;

public class AlgorithmicBattleStrategy implements BattleStrategy
{
	private Player attackingPlayer = null;
	private Player defendingPlayer =  null;
	private int attackingStrength = 0;
	private int defenseStrength	= 0;
	private int sidesOnDie = 6;
	
	// TODO Should be renamed. It runs a boolean not a unit, as the name suggest.
	public boolean getWinningUnit(GameContext g, Position from, Position to)
	{
		attackingPlayer = g.getPlayerInTurn();
		switch (g.getPlayerInTurn())
		{
			case RED : defendingPlayer = Player.BLUE;
				break;
			case BLUE : defendingPlayer = Player.RED;
				break;
		}
		
		attackingStrength = g.getUnitAt(from).getAttackingStrength();
		defenseStrength = g.getUnitAt(to).getDefensiveStrength();

		//Adding friendly support
		attackingStrength = attackingStrength + Utility.getFriendlySupport(g, from, attackingPlayer);
		defenseStrength = defenseStrength + Utility.getFriendlySupport(g, to, defendingPlayer);

		//Adding terrain support
		attackingStrength = attackingStrength * Utility.getTerrainFactor(g, from);
		defenseStrength = defenseStrength * Utility.getTerrainFactor(g, to);
		
		//Adding random support
		attackingStrength = attackingStrength * (getRandomNumber());
		defenseStrength = defenseStrength * (getRandomNumber());
		
		if(attackingStrength > defenseStrength)
		{
			return true;
		}
		return false;
	}
	protected int getRandomNumber()
	{
		return new Random().nextInt(sidesOnDie)+1;
	}
}
